//
//  HomelTableViewCell.swift
//  JKWeiBo
//
//  Created by 王冲 on 2017/12/10.
//  Copyright © 2017年 希爱欧科技有限公司. All rights reserved.
//

import UIKit
import SDWebImage

/**
 保存cell的重用标示
 
 - NormalCell:  原创微博的重用标识
 - ForwardCell: 转发微博的重用标识
 */
enum HomeTableViewCellIdentifier: String
{
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    // 如果在枚举中利用static修饰一个方法 , 相当于类中的class修饰方法
    // 如果调用枚举值的rawValue, 那么意味着拿到枚举对应的原始值
    static func cellID(status: HomeViewModel) ->String
    {
        return status.retweeted_status != nil ? ForwardCell.rawValue : NormalCell.rawValue
    }
}

class HomelTableViewCell: UITableViewCell {

    var homeViewModel: HomeViewModel?
    {
        didSet{
            
            // 给顶部赋值
            topView.topViewModel = homeViewModel
            
            // 正文
            /*
            label.text = homeViewModel?.text
            label.height = JKwideHighSize().JKtextStringSize(string: homeViewModel!.text! as NSString, size: CGSize(width:JKscreenW-24,height:1000), fontSize: 14).height
             */
            
            let attributedString:NSAttributedString = JKEmoticonAttributedString().backwards(str: (homeViewModel?.text)!, font: 14)!
            label.attributedText = attributedString
            label.height = JKwideHighSize().attributedStringSize(aString: attributedString, width: JKscreenW, height: CGFloat(MAXFLOAT), font: 14).height
            
            // 给配图赋值
            //homePictureView.homeViewModel = homeViewModel?.retweeted_status != nil ? homeViewModel?.retweeted_status : homeViewModel
            homePictureView.homeViewModel = homeViewModel
            
            let size = homePictureView.calculateImageSize3()
            homePictureView.height = size.height
            homePictureView.width = size.width
            
            homePictureView.y = homeViewModel?.storedPic_urls != nil ? label.frame.maxY + 10 : label.frame.maxY
            
            // 设置工具条的位置
            footview.y = homePictureView.frame.maxY+10
            
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
       
        self.setUpUI();
    }
    
    func setUpUI(){
      
       // 添加顶部
       contentView.addSubview(self.topView)
       // 添加正文
       contentView.addSubview(self.label)
       // 添加底部的工具条
       contentView.addSubview(self.footview)
       // 添加配图
       contentView.addSubview(self.homePictureView)
    
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    // MARK: - 懒加载
    // 1.正文以上的东西
    lazy var topView: HomelTableViewCellTopView = {
        
        let topview = HomelTableViewCellTopView()
        topview.frame = CGRect(x: 0, y: 0, width: JKscreenW, height: 74)
        return topview
        
    }()
   // 2.正文
    lazy var label: UILabel = {
        let label = UILabel.createlabel(color: UIColor.black, fontSize: 14)
        label.x = 12
        label.width = JKscreenW-24
        label.y = 74
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.left
        //label.backgroundColor = UIColor.JKRandomColor()
        return label
    }()
   // 3.底部工具条
    lazy var footview: HomelTableViewCellBottomView = {
        
        let footView = HomelTableViewCellBottomView()
        footView.width = JKscreenW
        footView.height = 44;
        footView.x = 0
        return footView
        
    }()
    
    // 8.配图的添加
    lazy var homePictureView: HomePictureView = {
        
        let homepicview = HomePictureView()
        homepicview.x = 10
        return homepicview
    }()
}

